<?php

namespace App\Http\Requests\Logic\Company;

use Illuminate\Foundation\Http\FormRequest;

class StoreResource extends FormRequest
{
	/**
	 * 验证用户是否已经有归属公司
	 *
	 * Date: 23/03/2018
	 * @author George
	 * @return bool
	 */
    public function authorize()
    {
    	$user = $this->user();

    	return $user->hasCompany() ? false : true;
    }

	/**
	 * 定义验证规则
	 *
	 * Date: 23/03/2018
	 * @author George
	 * @return array
	 */
    public function rules()
    {
        $rules = [
            'title' => 'required|unique:companies',
            'legal' => 'required',
            'city' => 'required',
            'address' => 'nullable',
            'telephone' => 'required',
            'email' => 'nullable|email',
            'certificate' => 'required',
            'introduction' => 'nullable',
        ];

        foreach ($this->all() as $key => $value) {
        	if (!array_has($rules, $key)) {
        		$this->offsetUnset($key);
			}
		}

		return $rules;
    }

	/**
	 * 定义验证错误消息
	 *
	 * Date: 23/03/2018
	 * @author George
	 * @return array
	 */
	public function messages()
	{
		return [
			'title.required' => '请输入公司名称',
			'title.unique' => '公司名称已存在',
			'legal.required' => '请输公司法人',
			'city.required' => '请选择公司所在城市',
			'telephone.required' => '请输入公司电话',
			'email.email' => '输入的公司邮箱有误',
			'certificate.required' => '请上传公司营业执照',
		];
    }
}
